package repository

import (
	"RocFamilySystem/models"
	"fmt"
	"gorm.io/gorm"
	"time"
)

type WorkTravelRepository struct {
	Db *gorm.DB
}

func init() {
	registerContainer["WorkTravelRepository"] = &WorkTravelRepository{}
}

func (r *WorkTravelRepository) SetDB(db *gorm.DB) {
	r.Db = db
}

// InsertWorkTravel 添加工作出差
func (r *WorkTravelRepository) InsertWorkTravel(workTravel *models.WorkTravel) {
	//开启事务
	db := r.Db.Begin()
	defer handleTransactional(db)
	err := db.Create(workTravel).Error
	handleRepositoryErr(err, "添加工作出差失败")
}

// DeleteWorkTravel 删除工作出差
func (r *WorkTravelRepository) DeleteWorkTravel(workTravelId int, loginUserId int) {
	db := r.Db.Begin()
	defer handleTransactional(db)
	err := db.Delete(&models.WorkTravel{Id: &workTravelId, CreateBy: &loginUserId}).Error
	db.Where("work_travel_id = ? and create_by = ?", workTravelId, loginUserId).Delete(models.WorkTravelPay{})
	handleRepositoryErr(err, "删除工作出差失败")
}

// UpdateWorkTravel 修改工作出差
func (r *WorkTravelRepository) UpdateWorkTravel(workTravel *models.WorkTravel, loginUserId int) {
	now := models.LocalTime(time.Now())
	workTravel.UpdateTime = &now
	err := r.Db.Model(workTravel).Omit("id", "create_time").Where("create_by = ?", loginUserId).Updates(workTravel).Error
	handleRepositoryErr(err, "修改工作出差失败")
}

// SelectWorkTravel 查询工作出差
func (r *WorkTravelRepository) SelectWorkTravel(workTravel *models.WorkTravel, pageNum, pageSize int, loginUserId int) ([]models.WorkTravel, int) {
	var (
		list  []models.WorkTravel
		total int64
	)

	db := r.Db.Model(&models.WorkTravel{})

	db = db.Where("create_by = ?", loginUserId)

	if workTravel != nil {
		if workTravel.WorkAddrr != nil {
			db = db.Where("work_addrr like ?", fmt.Sprintf("%%%s%%", *workTravel.WorkAddrr))
		}
		if workTravel.StartEndTime != nil {
			db = db.Where("start_end_time = ?", *workTravel.StartEndTime)
		}
	}

	//获取总数
	db.Count(&total)

	//获取列表
	db.Offset(pageSize * pageNum).Limit(pageSize)
	db.Find(&list)

	return list, int(total)
}

// SelectWorkTravelById 查询工作出差通过ID
func (r *WorkTravelRepository) SelectWorkTravelById(id int, loginUserId int) models.WorkTravel {
	var workTravel models.WorkTravel
	err := r.Db.Where("create_by = ?", loginUserId).First(&workTravel, "id = ?", id).Error
	if err != nil {
		handleRepositoryErr(err, "查询失败")
	}
	return workTravel
}

// SelectWorkTravelByIds 查询工作出差通过Ids
func (r *WorkTravelRepository) SelectWorkTravelByIds(ids []string) []models.WorkTravel {
	var workTravelList []models.WorkTravel
	err := r.Db.Find(&workTravelList, "id in (?)", ids).Error
	if err != nil {
		handleRepositoryErr(err, "查询失败")
	}
	return workTravelList
}

// InsertWorkTravelBatch 批量添加工作出差
func (r *WorkTravelRepository) InsertWorkTravelBatch(list []models.WorkTravel) {
	//开启事务
	db := r.Db.Begin()
	defer handleTransactional(db)
	err := db.Create(&list).Error
	handleRepositoryErr(err, "批量添加工作出差失败")
}

// DeleteWorkTravelBatch 批量删除工作出差
func (r *WorkTravelRepository) DeleteWorkTravelBatch(ids []string, loginUserId int) {
	db := r.Db.Begin()
	defer handleTransactional(db)
	err := db.Where("id in (?)  and create_by = ? ", ids, loginUserId).Delete(models.WorkTravel{}).Error
	handleRepositoryErr(err, "删除工作出差失败！")
}
